<script setup lang="ts">
import { request } from '@/api'
import { closeToast, showLoadingToast, type ActionSheetAction } from 'vant'
import { onMounted, ref } from 'vue'
//加载前运行
onMounted(async () => {
  //获取数据渲染
  //加载效果
  showLoadingToast({ message: '加载中...', duration: 0, forbidClick: true })
  //网络请求数据
  const res = await request.get<MkResponse<ProfileInfo>>('member/profile')
  //关闭加载效果
  closeToast()
  profile.value = res.data.result
  //地址获取
  const data = await mk.getAreaColumns()
  areaColumns.value = data
  console.log(data)
})

const showAvatarSheet = ref(false)
const showActions = [{ name: '拍照' }, { name: '图库' }]
const showdianji = (action: ActionSheetAction, index: number) => {
  if (index === 0) {
    console.log('拍照选择')
    mk.pickerCamera()
  } else if (index === 1) {
    console.log('图库选择')
    mk.pickerPhoto()
  }
}
//获取数据
const profile = ref<ProfileInfo>({} as ProfileInfo)
// 选择生日日期
const showBirthdayPopup = ref(false)
const onChangeBirthday = () => {
  showBirthdayPopup.value = false
}
//选择所在地
const showLocationPopup = ref(false)
const areaColumns = ref<AreaColumns>()
const onSelectedLocation = (data: { selectedValues: number[] }) => {
  showLocationPopup.value = false
  console.log(data)
}

//  选择职业
const columns = [
  { text: '软件工程师', value: '软件工程师' },
  { text: '医生', value: '医生' },
  { text: '教师', value: '教师' },
  { text: '律师', value: '律师' },
  { text: '会计师', value: '会计师' },
  { text: '销售经理', value: '销售经理' },
  { text: '市场营销专员', value: '市场营销专员' },
  { text: '建筑师', value: '建筑师' },
  { text: '护士', value: '护士' },
  { text: '机械工程师', value: '机械工程师' },
]

const showProfessionPopup = ref(false)
const onChangeProfession = (selectedValues: string[]) => {
  showProfessionPopup.value = false
}
</script>

<template>
  <div class="profile-edit-page">
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image
        :src="profile.avatar"
        round
        width="100"
        height="100"
        class="avatar-img"
        @click="showAvatarSheet = true"
      >
      </van-image>
      <div class="avatar-btn">
        <span @click="showAvatarSheet = true">修改头像</span>
      </div>
      <van-action-sheet
        @select="showdianji"
        style="padding-bottom: 20px"
        v-model:show="showAvatarSheet"
        :actions="showActions"
        cancel-text="取消"
        close-on-click-action
      />
    </div>

    <!-- 中间表单部分 -->
    <van-cell-group>
      <van-field label="账号" v-model="profile.account" readonly></van-field>
      <van-field
        label="昵称"
        placeholder="请输入昵称"
        v-model="profile.nickname"
      ></van-field>
      <van-cell title="性别" class="gender">
        <van-radio-group :icon-size="16" v-model="profile.gender">
          <van-radio name="男">男</van-radio>
          <van-radio name="女">女</van-radio>
          <van-radio name="未知">未知</van-radio>
        </van-radio-group>
      </van-cell>
      <!-- 生日日期 -->
      <van-field
        label="生日"
        readonly
        placeholder="请选择日期"
        v-model="profile.birthday"
        @click="showBirthdayPopup = true"
      ></van-field>
      <!-- 生日弹出层 -->
      <van-popup v-model:show="showBirthdayPopup" position="bottom">
        <van-date-picker
          title="选择日期"
          @cancel="showBirthdayPopup = false"
          @confirm="onChangeBirthday"
          :min-date="new Date('1980-01-01')"
          :max-date="new Date()"
        />
      </van-popup>
      <!-- 所在地 -->
      <van-field
        label="所在地"
        readonly
        placeholder="请选择所在地"
        v-model="profile.fullLocation"
        @click="showLocationPopup = true"
      >
      </van-field>
      <!-- 地址弹出层 -->
      <van-popup v-model:show="showLocationPopup" round position="bottom">
        <van-area
          :area-list="areaColumns"
          @confirm="onSelectedLocation"
        ></van-area>
      </van-popup>
      <!-- 职业选择 -->
      <van-field
        label="职业"
        readonly
        placeholder="请选择职业"
        v-model="profile.profession"
        @click="showProfessionPopup = true"
      ></van-field>
      <!-- 职业弹出层 -->
      <van-popup v-model:show="showProfessionPopup" position="bottom">
        <van-picker
          title="标题"
          :columns="columns"
          @cancel="showProfessionPopup = false"
          @confirm="onChangeProfession"
        />
      </van-popup>
    </van-cell-group>

    <div class="submit">
      <van-button round block type="primary"> 保存资料 </van-button>
    </div>
  </div>
</template>
<style lang="css" scoped>
.avatar {
  padding: 30px;
  text-align: center;
  background-color: var(--mk-white);
}

.avatar-img {
  box-shadow: 0 0 5px var(--mk-gray);
}

.avatar-btn {
  color: var(--mk-linear_end);
  margin-top: 10px;
}

.gender ::v-deep(.van-cell__title) {
  width: 100px;
  flex: none;
}

.van-radio-group {
  display: flex;
  justify-content: space-between;
  height: 36px;
  width: 180px;
}

.submit {
  padding: 16px;
}
</style>
